<template>
    <el-breadcrumb class="h-[50px] flex items-center">
        <transition-group name="breadcrumb">
            <el-breadcrumb-item v-for="(item, index) in breadcrumbs" :key="item.path">
                <span
                    v-if="item.redirect === 'noredirect' || index === breadcrumbs.length - 1"
                    class="text-[var(--el-disabled-text-color)]"
                    >{{ item.meta.title }}</span
                >
                <a v-else @click.prevent="handleLink(item)">
                    {{ item.meta.title }}
                </a>
            </el-breadcrumb-item>
        </transition-group>
    </el-breadcrumb>
</template>

<script setup lang="ts">
import { compile } from 'path-to-regexp';
import { onBeforeMount, ref, watch } from 'vue';
import { RouteLocationMatched, useRoute } from 'vue-router';

import router from '@/router';
// import { translateRouteTitle } from '@/utils/i18n';

const currentRoute = useRoute();
const pathCompile = (path: string) => {
    const { params } = currentRoute;
    const toPath = compile(path);
    return toPath(params);
};

const breadcrumbs = ref([] as Array<RouteLocationMatched>);

function getBreadcrumb(meta) {
    let matched = currentRoute.matched.filter((item) => item.meta && item.meta.title);
    const first = matched[0];
    if (meta) {
        matched[0].meta.title = meta.title;
    }
    if (!isDashboard(first)) {
        matched = [{ path: '/', meta: { title: '首页' } } as any].concat(matched);
    }
    breadcrumbs.value = matched.filter((item) => {
        return item.meta && item.meta.title && item.meta.breadcrumb !== false;
    });
}

function isDashboard(route: RouteLocationMatched) {
    const name = route && route.name;
    if (!name) {
        return false;
    }
    return name.toString().trim().toLocaleLowerCase() === 'Dashboard'.toLocaleLowerCase();
}

function handleLink(item: any) {
    const { redirect, path } = item;
    if (redirect) {
        router.push(redirect).catch((err) => {
            console.warn(err);
        });
        return;
    }
    router.push(pathCompile(path)).catch((err) => {
        console.warn(err);
    });
}

watch(
    () => currentRoute.path,
    (path) => {
        if (path.startsWith('/redirect/')) {
            return;
        }
        getBreadcrumb(undefined);
    },
);
watch(
    () => currentRoute.meta,
    (meta) => {
        getBreadcrumb(meta);
    },
);
onBeforeMount(() => {
    getBreadcrumb(undefined);
});
</script>

<style lang="less" scoped>
.app-breadcrumb.el-breadcrumb {
    display: inline-block;
    margin-left: 8px;
    font-size: 14px;
    line-height: 50px;
}

// 覆盖 element-plus 的样式
.el-breadcrumb__inner,
.el-breadcrumb__inner a {
    font-weight: 400 !important;
}
</style>
